package fresher.servletcontroller;

import fresher.model.BillTotalModel;
import fresher.model.ExampleModel;
import fresher.objects.Bill;
import fresher.utility.DateConvertUtility;
import fresher.utility.TemplateManager;
import fresher.utility.ValidateUtility;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import hapax.TemplateException;
import hapax.TemplateLoader;
import hapax.TemplateResourceLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TotalServletController extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        TemplateDataDictionary dictionary = TemplateDictionary.create();

        //Process business logic
        String beginValue = request.getParameter("begin");
        if (!ValidateUtility.checkNull(beginValue)) {
            ErrorServletController.showError(request, response, "Begin date is null!");
            return;
        }

        String finishValue = request.getParameter("finish");
        if (!ValidateUtility.checkNull(finishValue)) {
            ErrorServletController.showError(request, response, "Finish date is null!");
            return;
        }


        Date begin = DateConvertUtility.toDate(beginValue);
        Date finish = DateConvertUtility.toDate(finishValue);
        BillTotalModel billTotalModel = new BillTotalModel();
        ArrayList<Bill> list = billTotalModel.getBill(begin, finish);
        long result = 0;
        for (int i = 0; i < list.size(); i++) {
            result += list.get(i).getTotal();
        }

        dictionary.setVariable("result", Long.toString(result));
        dictionary.setVariable("begin", beginValue);
        dictionary.setVariable("finish", finishValue);

        for (int i = 0; i < list.size(); i++) {
            //create a section to store object
            TemplateDataDictionary bills = dictionary.addSection("bills");

            //set variable to sectionw
            Bill current = list.get(i);
            bills.setVariable("bills.id", Integer.toString(current.getId()));
            bills.setVariable("bills.tableId", Integer.toString(current.getTableId()));
            bills.setVariable("bills.date", DateConvertUtility.toString(current.getDate()));
            bills.setVariable("bills.total", Integer.toString(current.getTotal()));
        }
        //Render output 
        String content = "";
        try {
            content = TemplateManager.getInstance().getTemplate("/view/total_result.xtm").renderToString(dictionary);
        } catch (TemplateException ex) {
            ErrorServletController.showError(request, response, ex.getMessage());
        }

        //Réponse
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(content);
    }
}